﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iLangl.Model.Rules
{
	public class ValidateTranslation : IValidation
	{
		Translation translation;
		#region IValidation Members

		public IList<IError> Validate(object item)
		{
			List<IError> result = new List<IError>();

			if (!(item is Translation))
			{
				result.Add(new Error(
					"Validation Failed",
					String.Format("Expected type ='Translation' instead of {0}", item.GetType()),
					eSeverity.High));

				return result;
			}

			translation = item as Translation;


			if (String.IsNullOrEmpty(translation.Id))
			{
				
				result.Add(new Error(
				"Translation Identity is not specified!",
				string.Format("Translation.Id is required field.{0}!", 
				(translation.Project == null)? 
					string.Empty: 
					translation.Project.ToString()),
				eSeverity.Critical));
			}

			if (translation.SourceContent == null)
			{
				result.Add(new Error(
				"Source Content is not specified!",
				string.Format("Translation.Source Content is required field.{0}!",
				(translation.Project == null) ?
					string.Empty :
					translation.Project.ToString()),
				eSeverity.High));
			}


			if (
				((int)ProjectStatuses.GetStatus(translation.Project.Status) >= (int)ProjectStatuses.eStatus.ReadyToStart)
				&& (translation.Responsible == null))
			{
				result.Add(new Error(
				"Translation Responsible is not specified!",
				"Translation.Responsible is requied field when Translation already is started!",
				eSeverity.Medium));
			}

			if (translation.LanguagePair == null)
			{
				result.Add(new Error(
				"Language Pair is not specified!",
				string.Format("Translation.Language Pair is required field.{0}!",
				(translation.Project == null) ?
					string.Empty :
					translation.Project.ToString()),
				eSeverity.High));
			}

			if ((translation.History == null) || (translation.History.Length <=0))
			{
				result.Add(new Error(
				"History is not specified!",
				string.Format("History is required field.{0}!",
				(translation.Project == null) ?
					string.Empty :
					translation.Project.ToString()),
				eSeverity.Medium));
			}
			else
			{
				ValidateStateHistory vhistory = new ValidateStateHistory();
				foreach (StateHistory shistory in translation.History)
				{
					result.AddRange(vhistory.Validate(shistory));
				}
			}

			return result;
		}

		#endregion
	}
}
